<?php

namespace app\user\controller;

use app\common\controller\User;
use app\common\serve\Buy;

class Invested extends User
{

    //修改资料
    public function index()
    {

        $list = model('BorrowInvest')
            ->where(['invest_uid' => $this->uid, 'status' => 1])
            ->order('id DESC')
            ->paginate(7, $this->isMobile() ? true : false, $this->request->param());

        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }


    public function receive()
    {
        $list = model('BorrowInvest')
            ->where(['invest_uid' => $this->uid, 'status' => ['IN', [2, 3]]])
            ->order('id DESC')
            ->paginate(7, $this->isMobile() ? true : false, $this->request->param());

        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    public function done()
    {

        $list = model('BorrowInvest')
            ->where(['invest_uid' => $this->uid, 'status' => 4])
            ->order('id DESC')
            ->paginate(7, $this->isMobile() ? true : false, $this->request->param());

        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }


    /**
     * 确认下单
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function ready()
    {
        if ($this->request->isPost()) {

            $id = $this->request->post('id');
            if ($id < 1) $this->error('参数缺少！');

            $result = Buy::readyPay([
                'uid' => $this->uid,
                'id' => $id,
            ]);

            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg'], url('receive'));
            } else {
                $this->error($result['msg'], '');
            }


        }

        $this->redirect('/');
    }

    /**
     * 出售
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function sale()
    {

        if ($this->request->isPost()) {

            $id = $this->request->param('id');
            if ($id < 1) $this->error('参数出错！');
            $result = Buy::sale([
                'uid' => $this->uid,
                'id' => $id,
            ]);

            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg'], url('done'));
            } else {
                $this->error($result['msg'], '');
            }

        }

        $this->redirect('/user');
    }
}
